package com.lsk.serviceDriverUser.service;

import com.lsk.internalcommon.DTO.Car;
import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.response.TerminalResponse;
import com.lsk.internalcommon.response.TraceResponse;
import com.lsk.serviceDriverUser.mapper.CarMapper;
import com.lsk.serviceDriverUser.remote.ServiceMapClient;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@Service
public class CarService {
    @Autowired
    private CarMapper carMapper;

    @Autowired
    private ServiceMapClient serviceMapClient;

    public ResponseResult addCar(Car car){
        LocalDateTime now = LocalDateTime.now();
        car.setGmtCreate(now);
        car.setGmtModified(now);
        //获取tid
        ResponseResult<TerminalResponse> responseResult = serviceMapClient.addTerminal(car.getVehicleNo());
        String tid = responseResult.getData().getTid();
        //获取trid
        ResponseResult<TraceResponse> traceResponse = serviceMapClient.addTrace(tid);
        String trid = traceResponse.getData().getTrid();
        String trname = traceResponse.getData().getTrname();
        car.setTid(tid);
        car.setTrid(trid);
        car.setTrname(trname);
        carMapper.insert(car);
        return ResponseResult.success("新增成功");
    }

    public ResponseResult getCarById(Long carId){
        HashMap<String, Object> map = new HashMap<>();
        map.put("id",carId);
        List<Car> cars = carMapper.selectByMap(map);
        if (cars.size()==0){
            return ResponseResult.fail("根据carId获取车辆失败");
        }
        return ResponseResult.success(cars.get(0));
    }
}
